/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package beans;

import dbs.Friend;
import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;

/**
 *
 */
@Stateless
@LocalBean
public class FollowBean {
    
    @EJB
    private FriendFacade friendFacade;

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
   
    public boolean followFriend(long userId, long friendId) {
        Friend friend1 = friendFacade.findFriendData(userId, friendId);
        Friend friend2 = friendFacade.findFriendData(friendId, userId);
        if(friend1 == null || friend2 == null)
        {
            return false;
        }
        friend1.setFollowingFlag((short)1);
        friend2.setFollowerFlag((short)1);
        
        friendFacade.edit(friend1);
        friendFacade.edit(friend2);
        
        return true;
    }

    public boolean unfollowFriend(long userId, long friendId) {
        Friend friend1 = friendFacade.findFriendData(userId, friendId);
        Friend friend2 = friendFacade.findFriendData(friendId, userId);
        if(friend1 == null || friend2 == null)
        {
            return false;
        }
        friend1.setFollowingFlag((short)0);
        friend2.setFollowerFlag((short)0);
        
        friendFacade.edit(friend1);
        friendFacade.edit(friend2);
        
        return true;
    }
}
